<?php
namespace app\admin\controller;

use app\common\BaseAdminController;
use app\common\Result;
use app\model\Admin;
use app\model\Store as ModelStore;

class store extends BaseAdminController
{
    public function list($pageNum = 1, $pageSize = 10, $name = "")
    {
        $list  = ModelStore::where('name', 'like', "%{$name}%")->page($pageNum, $pageSize)->select();
        $count = ModelStore::where('name', 'like', "%{$name}%")->count();
        return Result::success([
            'list'  => $list,
            'total' => $count,
        ], '获取列表成功');
    }

    public function add()
    {
        $data = $this->request->post();

        $validate = new \app\validate\Store();
        if (! $validate->check($data)) {
            return Result::error($validate->getError());
        }

        $store          = new ModelStore();
        $store->name    = $data['name'];
        $store->address = $data['address'];
        $store->phone   = $data['phone'];
        $store->image   = $data['image'];
        $store->status  = 0;
        $store->save();

        return Result::success(null, '添加成功');
    }

    public function detail($id = '')
    {
        if (! $id) {
            return Result::error('id不能为空');
        }
        // 查询店铺信息并关联出所有管理员信息
        $store = ModelStore::with('admins')->find($id);
        if (! $store) {
            return Result::error('店铺不存在');
        }
        return Result::success($store, '获取详情成功');
    }

    public function bindAdmin()
    {
        $data    = $this->request->post();
        $storeId = $data['storeId'];
        $adminId = $data['adminId'];

        $arr = explode(',', $adminId); // 分割管理员ID

        $store = ModelStore::find($storeId);
        if (! $store) {
            return Result::error('店铺不存在');
        }

        // 开启事务
        try {
            // 查询管理员信息并检查是否已绑定其他店铺
            $adminList = Admin::where('id', 'in', $arr)
                ->where(function ($query) {
                    $query->where('store_id', null);
                })
                ->select();

            if (count($adminList) != count($arr)) {
                return Result::error('部分管理员已绑定其他店铺');
            }

            // 先解绑店铺原有管理员
            Admin::where('store_id', $storeId)->update(['store_id' => null]);
            // 绑定新管理员
            Admin::where('id', 'in', $arr)->update(['store_id' => $storeId]);
            return Result::success(null, '绑定成功');
        } catch (\Exception $e) {
            return Result::error('绑定失败:' . $e->getMessage());
        }
    }

    public function edit()
    {
        $data = $this->request->post();

        $validate = new \app\validate\Store();
        if (! $validate->check($data)) {
            return Result::error($validate->getError());
        }

        // 判断name是否重复
        $store = ModelStore::where('name', $data['name'])->where('id', '<>', $data['id'])->find();
        if ($store) {
            return Result::error('店铺名称已存在');
        }

        $store = ModelStore::find($data['id']);

        if (! $store) {
            return Result::error('店铺不存在');
        }

        $store->name    = $data['name'];
        $store->address = $data['address'];
        $store->phone   = $data['phone'];
        $store->image   = $data['image'];
        $store->save();

        return Result::success(null, '店铺信息更新成功');
    }

    public function audit()
    {
        $data = $this->request->post();

        $validate = new \app\validate\Audit();
        if (! $validate->check($data)) {
            return json(['code' => 400, 'msg' => $validate->getError()]);
        }

        $store = ModelStore::find($data['storeId']);
        if (! $store) {
            return Result::error('店铺不存在');
        }

        $store->status = $data['status'];

        if ($data['status'] == 2) {
            $store->reason = $data['reason'];
        }

        $store->save();

        return Result::success(null, '审核成功');
    }
}
